﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BCZadmin.Helper;
using BCZadmin.Model;
using System.Runtime.InteropServices;

namespace BCZadmin
{
    public partial class Form1 : Form
    {        
        //窗体弹出或消失效果
        [DllImport("user32.dll", EntryPoint = "AnimateWindow")]
        private static extern bool AnimateWindow(IntPtr handle, int ms, int flags);
        public const Int32 AW_HOR_POSITIVE = 0x00000001;
        public const Int32 AW_HOR_NEGATIVE = 0x00000002;
        public const Int32 AW_VER_POSITIVE = 0x00000004;
        public const Int32 AW_VER_NEGATIVE = 0x00000008;
        public const Int32 AW_CENTER = 0x00000010;
        public const Int32 AW_HIDE = 0x00010000;
        public const Int32 AW_ACTIVATE = 0x00020000;
        public const Int32 AW_SLIDE = 0x00040000;
        public const Int32 AW_BLEND = 0x00080000;
        //窗体弹出或消失效果

        [DllImport("user32.dll", EntryPoint = "SendMessageA")]
        private static extern int SendMessage(int hwnd, int wMsg, int wParam, int lParam);

        [DllImport("user32.dll")]
        private static extern int ReleaseCapture();
        private const int WM_NCLBUTTONDOWN = 0XA1;   //.定义鼠標左鍵按下
        private const int HTCAPTION = 2;
        private const string BaseUrl = "http://123.207.86.30/public/index.php/db";
        DocumentHelp document = new DocumentHelp(Application.StartupPath + @"\record.config");
        public Form1()
        {
            InitializeComponent();
        }

        private void BT_Login_Click(object sender, EventArgs e)
        {
            //HttpContext.Current.Request.Cookies["cookieName"]
            if (CB_id.Text == "" || TB_pwd.Text == "")
            {
                (new Message("账号或密码不能为空")).Show();
                return;
            }
            Dictionary<string, string> userlogin = new Dictionary<string, string>();
            userlogin.Add("id", CB_id.Text);
            userlogin.Add("pwd", TB_pwd.Text);
            var redate = HttpHelper.GetResponseString(HttpHelper.CreatePostHttpResponse(BaseUrl + "/Admincontrol/login", userlogin, 100, "", null));
            switch (redate)
            {
                case "": (new Message("登录失败")).Show(); return;
                case "error pwd": (new Message("登录失败")).Show(); return;
                default:
                    AdminInf NumberInf = JSONHelper.JsonDeserialize<AdminInf>(redate);
                    if (CB_remenber.Checked) Remenber(NumberInf); else Forget(NumberInf);
                    {
                        AnimateWindow(this.Handle, 300, AW_HIDE + AW_CENTER);
                        (new Main(NumberInf)).ShowDialog();
                    }
                    break;
            }
        }
        private void BT_out_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void Remenber(AdminInf NumberInf)
        {
            document.Write(NumberInf.A_LoginId, NumberInf.A_Pwd);
        }
        private void Forget(AdminInf NumberInf)
        {

        }
        private void Form1_Load(object sender, EventArgs e)
        {
            document.Read();
            if (document.rd == null)
                return;
            foreach (string id in document.rd.Keys)
            {
                CB_id.Items.Add(id);
            }
            CB_id.Text = CB_id.Items[0].ToString();
            CB_remenber.Checked = true;
            AnimateWindow(this.Handle, 300, AW_CENTER);//窗体弹出效果
        }
        private void CB_id_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (document.rd == null)
                return;
            foreach (string id in document.rd.Keys)
            {
                if (id == CB_id.Text)
                    TB_pwd.Text = document.rd[id];
            }
        }
        private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //為當前的應用程序釋放鼠標鋪獲
            ReleaseCapture();
            //發送消息﹐讓系統誤以為在标题栏上按下鼠標
            SendMessage((int)this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
        }

    }
}
